# 从右往左遍历
def lengthOfLastWord(self, s: str) -> int:
    if len(s) == 0: return 0
    end = len(s) - 1
    while end >= 0 and s[end] == ' ': end -= 1
    if end < 0: return 0
    start = end
    while start >= 0 and s[start] != ' ': start -= 1
    return end - start

# 从左往右遍历
def lengthOfLastWord(self, s: str) -> int:
    ans = 0
    start, end = 0, 0
    while end < len(s):
        if s[start] == ' ':
            start += 1
            end += 1
        else:
            while end < len(s) and s[end] != ' ':
                end += 1
            ans = end - start
            while end < len(s) and s[end] == ' ':
                end += 1
            if end < len(s) and s[end] != ' ':
                start = end
    return ans
